package autobox::REST;

use warnings;
use strict;

use autobox;
use LWP::UserAgent;
use HTTP::Request;

our $VERSION = '0.01';
our $PROXY_TIMEOUT = 10;
our $PROXY;

sub SCALAR::get {
    my $uri = shift;
    my $request_content = shift;

    my $response = $uri->_request('GET',$request_content);
    return $response;
}

sub SCALAR::post {
    my $uri = shift;
    my $request_content = shift;

    my $response = $uri->_request('POST',$request_content);
    return $response;
}

sub SCALAR::put {
    my $uri = shift;
    my $request_content = shift;

    my $response = $uri->_request('PUT',$request_content);
    return $response;
}

sub SCALAR::delete {
    my $uri = shift;
    my $request_content = shift;

    my $response = $uri->_request('DELETE',$request_content);
    return $response;
}

sub SCALAR::_request{
    my $uri     = shift;
    my $request_method  = shift;
    my $request_content = shift;

    my $request = HTTP::Request->new($request_method => $uri);

    $request->content($request_content)
      if( $request_content );

    my $ua = LWP::UserAgent->new;

#    $ua->timeout(10);
#    $ua->proxy(['http','ftp'], 'http://proxy.internal:8080/');
    if( $PROXY ){
        $ua->timeout($PROXY_TIMEOUT);
        $ua->proxy(['http','ftp'], $PROXY );
    }

    my $response = $ua->request($request);
    if ( $response->is_success ) {
        return $response;
    }else{
        warn $response->status_line,"\n";
        return $response;
    }
}


1;

=head1 NAME

autobox::REST - 

=head1 SYNOPSIS


    use autobox;
    use autobox::REST;
    
    my $response = "http://example.com/rest_api/"->post("foo=1&bar=hoo")

    "http://example.com/rest_api/"->get();

    "http://example.com/rest_api/"->put("foo=100");

    "http://example.com/rest_api/"->delete();

=head1 DESCRIPTION

This module calls the uri by HTTP METHOD(POST,GET,PUT,DELETE).

=head1 METHODS

=head2 post

This method calls the uri by POST, and returns a L<HTTP::Response> object.

=head2 get

This method calls the uri by GET, and returns a L<HTTP::Response> object.

=head2 put

This method calls the uri by PUT, and returns a L<HTTP::Response> object.

=head2 delete

This method calls the uri by DELETE, and returns a L<HTTP::Response> object.

=head1

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

=head1 AUTHOR

Akihito Takeda, C<< <takeda.akihito at gmail.com> >>

=cut

